package org.edu.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.edu.service.PurviewTreeNodeService;
import org.edu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
/**
 * <h2>角色Action</h2>
 * <p>已在方法上注明方法作用</p>
 * @author 谢圣强
 * @version 2.0
 */
@Controller
@ParentPackage("struts-default")
@Namespace("/role")
public class RoleAction extends ActionSupport implements RequestAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8638609293139751623L;
	private static final Logger logger = Logger.getLogger(RoleAction.class);
	private Map<String, Object> request;
	private String roleid;
	private String purviewid;
	private String rolename;
	@Autowired
	private RoleService roleService;
	@Autowired
	private PurviewTreeNodeService purviewTreeNodeService;
	/**
	 * 修改角色权限
	 * @return
	 */
	@Action(value="editrole",results={@Result(name="success",type="chain",location="allrole")})
	public String editRole(){
		try{
		logger.info("管理员修改"+roleid+"的权限");
		System.out.println(purviewid);
		roleService.update(purviewid, roleid);
		}catch(NumberFormatException e){
			request.put("tip", "修改权限失败，失败原因未提交正常的权限格式");
			logger.info("修改权限失败，失败原因未提交正常的权限格式");
		}catch (Exception e) {
			request.put("tip", "修改权限失败，数据库异常");
			logger.info("修改权限失败，数据库异常");
		}
		return SUCCESS;
	}
	/**
	 *  获取全部角色信息
	 * @return
	 */
	@Action(value="allrole",results={@Result(name="success",location="/examples/editRole.jsp")})
	public String allrole(){
		logger.info("获取全部角色");
		try{
		request.put("rolelist", roleService.list());
		}catch(Exception e){
			request.put("tip", "获取角色失败");
			logger.info("获取角色失败");
		}
		return SUCCESS;
	}
	/**
	 * 新建角色
	 * @return
	 */
	@Action(value="addrole",results={@Result(name="success",type="chain",location="allrole")})
	public String addrole(){
		try {
			logger.info("新建角色，名称为："+URLDecoder.decode(rolename, "utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try{
		request.put("roleid",roleService.addRole(URLDecoder.decode(rolename, "utf-8")));
		request.put("iseditpurview","true");
		}catch( Exception e){
			request.put("tip","新建角色失败");
			logger.info("新建角色失败");
		}
		
		
		return SUCCESS;
	}
	/**
	 * 删除角色
	 * @return
	 */
	@Action(value="delrole",results={@Result(name="success",type="chain",location="allrole")})
	public String delrole(){
		logger.info("删除角色，id为："+roleid);
		try{
			//request.put("roleid",roleService.addRole(rolename));
			roleService.delete(roleid);
			
		}catch( Exception e){
			request.put("tip","删除角色失败");
			logger.info("删除角色失败");
		}
		
		
		return SUCCESS;
	}
	/**
	 * 修改角色名称
	 * @return
	 */
	@Action(value="eidtrolename",results={@Result(name="success",type="chain",location="allrole")})
	public String eidtrolename(){
		logger.info("修改角色，id为："+roleid);
		try{
			//request.put("roleid",roleService.addRole(rolename));
			roleService.updataName(URLDecoder.decode(rolename, "utf-8"), roleid);
			
		}catch( Exception e){
			request.put("tip","修改角色失败");
			logger.info("修改角色名称失败");
		}
		return SUCCESS;
	}
	public Map<String, Object> getRequest() {
		return request;
	}
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	public String getRoleid() {
		return roleid;
	}
	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}
	public String getPurviewid() {
		return purviewid;
	}
	public void setPurviewid(String purviewid) {
		this.purviewid = purviewid;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}
}
